#!/usr/bin/env python3
#-*- coding: utf-8 -*-
from pygame.sprite import Group

from ship import Ship

__auther__ = 'teffy'

'''
积分榜功能
@filename scoreboard
@created on 2017-11-14
@poject:python-shoot-game
@auther:teffy
'''

import pygame.ftfont

class Scoreboard():

    def __init__(self,settings,screen,stats):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.settings = settings
        self.stats = stats

        self.text_color = (30,30,30)
        self.font = pygame.ftfont.SysFont(None,48)

        self.prep_score()
        self.prep_high_score()
        self.prep_level()
        self.prep_ships()

    def prep_high_score(self):
        high_score = int(round(self.stats.high_score,-1))
        high_score_str = '{:,}'.format(high_score)
        self.high_score_img = self.font.render(high_score_str,True,self.text_color,self.settings.bg_color)

        self.high_score_rect = self.high_score_img.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.screen_rect.top

    def prep_score(self):
        '''
        # 函数 round() 通常让小数精确到小数点后多少位,其中小数位数是由第二个实参指定的。然
        而,如果将第二个实参指定为负数, round() 将圆整到最近的10、100、1000等整数倍。这里的代
        码让Python将 stats.score 的值圆整到最近的10的整数倍,并将结果存储到 rounded_score 中。
        '''
        rounded_score = int(round(self.stats.score,-1))
        score_str = '{:,}'.format(rounded_score)
        self.score_img = self.font.render(score_str,True,self.text_color,self.settings.bg_color)

        self.score_rect = self.score_img.get_rect()
        self.score_rect.right = self.screen_rect.right  - 20
        self.score_rect.top = 20

    def prep_level(self):
        level_str = str(self.stats.level)
        self.level_img = self.font.render(level_str,True,self.text_color,self.settings.bg_color)
        self.level_rect = self.level_img.get_rect()
        self.level_rect.right = self.screen_rect.right - 20
        self.level_rect.top = self.score_rect.bottom + 10

    def show_score(self):
        self.screen.blit(self.score_img,self.score_rect)
        self.screen.blit(self.level_img,self.level_rect)
        self.screen.blit(self.high_score_img,self.high_score_rect)
        self.ships.draw(self.screen)

    def prep_ships(self):
        self.ships = Group()
        for si in range(self.stats.ship_left):
            ship = Ship(self.screen,self.settings)
            ship.rect.x = 10 + si * ship.rect.width
            ship.rect.y = 10
            self.ships.add(ship)





